package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.views.AdminPanelView;
import ch.fhnw.qualiscan.core.Modal;

import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;

import eu.livotov.tpt.i18n.TM;

public class AdminAddUniversity extends Modal {
	
	private Form editForm;
	private AdminModel adminModel;
	private AdminPanelView caller;
	
	public AdminAddUniversity(AdminPanelView caller) {
		setCaption(TM.get("admin.adduniversity.title"));
		
		this.caller = caller;
		adminModel = new AdminModel();
		buildLayout();
	}
	
	public void buildLayout() {
		editForm = new Form();
		HorizontalLayout buttonBar = new HorizontalLayout();
		editForm.getFooter().addComponent(buttonBar);
		editForm.setCaption(TM.get("admin.adduniversity.title"));
		
		TextField titleField = new TextField(); 
		titleField.setColumns(20);
		titleField.setWidth("100%");
		titleField.isRequired();
		titleField.setCaption(TM.get("admin.universitytable.title"));
		editForm.addField("titleField", titleField);
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editForm.commit();
				String title = (String)editForm.getField("titleField").getValue();
				adminModel.saveUniversity(title);
				
				AdminAddUniversity aau = (AdminAddUniversity) event.getButton().getData();
				aau.close();
				
				caller.buildLayout();
			}
		});
		
		saveButton.setData(this);
		saveButton.setIcon(Config.ICON_SAVE);
		buttonBar.addComponent(saveButton);
		addComponent(editForm);
	}
	
}
